<template>
  <div class="app-container">
    <div class="content-container">
      <div class="search-bar">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item prop="keywords" label="姓名">
            <el-input v-model="queryParams.ownName" placeholder="请输入姓名" clearable />
          </el-form-item>
          <!-- <el-form-item prop="keywords" label="联系方式">
            <el-input v-model="queryParams.mobile" placeholder="请输入联系方式" clearable />
          </el-form-item> -->

          <el-form-item>
            <el-button type="primary" icon="search" @click="handleSearch">搜索</el-button>
            <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-card shadow="never" class="table-card">
        <div class="table-card-body">
          <BasePageTable
            :table-data="tableData"
            :table-columns-data="tableColumnsData"
            :total="total"
            :loading="loading"
            @handleQuery="changePage"
          >
            <template #type="{ row, name }">
              {{ getLabelByValue(CONTACT_TYPE, row.type) }}
            </template>
            <template #status="{ row, name }">
              <el-tag :type="BIND_STATUS_ENUM.SUCCESS.value === row.status ? 'success' : 'info'">
                {{ getLabelByValue(CONTACT_BIND_STATUS_ENUM, row.status) }}
              </el-tag>
            </template>
            <template #operation="{ row, name }">
              <el-button
                link
                type="danger"
                icon="Close"
                :disabled="
                  row.status === BIND_STATUS_ENUM.SUCCESS.value ||
                  row.status === BIND_STATUS_ENUM.FAILURE.value
                "
                @click="handleApproval(row, BIND_STATUS_ENUM.FAILURE.value)"
              >
                拒绝
              </el-button>
              <el-button
                link
                type="primary"
                icon="Check"
                :disabled="
                  row.status === BIND_STATUS_ENUM.SUCCESS.value ||
                  row.status === BIND_STATUS_ENUM.FAILURE.value
                "
                @click="handleApproval(row, BIND_STATUS_ENUM.SUCCESS.value)"
              >
                同意
              </el-button>
            </template>
          </BasePageTable>
        </div>
      </el-card>
    </div>
    <user-form
      :id="currentRow.id"
      v-model="formVsisble"
      :data="currentRow"
      @load-data="handleQuery"
    ></user-form>
  </div>
</template>

<script setup lang="ts">
import BasePageTable from "@/components/BasePageTable/index.vue";
import HouseAPI, { Approval, HousePageQuery, HousePageVO } from "@/api/house/index";
import UserForm from "@/views/system/employee/components/user-form.vue";
import { Message } from "@element-plus/icons-vue";
import { getLabelByValue } from "@/utils/enums";
import { CONTACT_TYPE, GenderEnum, UserStatusEnum } from "../../../enums/system/UserEnum";
import { BIND_STATUS_ENUM, CONTACT_BIND_STATUS_ENUM } from "@/enums/contact/ContactEnum";

const loading = ref(false);
const formVsisble = ref(false);
const total = ref(0);
const queryFormRef = ref();
const currentRow = ref({});

const queryParams = reactive<HousePageQuery>({
  ownName: "",
  mobile: "",
  pageNum: 1,
  pageSize: 10,
});
const tableData = ref<HousePageVO[]>([]);
const tableColumnsData = ref([
  {
    prop: "house.ownName",
    label: "户主名称",
    showOverflowTooltip: true,
  },
  {
    prop: "house.region",
    label: "小区地址",
    showOverflowTooltip: true,
  },
  {
    prop: "house.neighborhood",
    label: "小区名称",
    showOverflowTooltip: true,
  },
  {
    prop: "house.houseNumber",
    label: "门牌号",
    width: 100,
    showOverflowTooltip: true,
  },
  {
    prop: "house.size",
    label: "面积（平方米）",
    width: 150,
    showOverflowTooltip: true,
  },
  {
    prop: "type",
    label: "通讯录类型",
    width: 150,
    showOverflowTooltip: true,
    customSlot: "type",
  },
  {
    prop: "status",
    label: "状态",
    width: 150,
    showOverflowTooltip: true,
    customSlot: "status",
  },
  {
    prop: "createTime",
    label: "创建时间",
    width: 150,
    showOverflowTooltip: true,
  },
  {
    prop: "operation",
    label: "操作",
    width: 200,
    align: "center",
    customSlot: "operation", // 自定义插槽名称
  },
]);
// 查询
function handleQuery() {
  loading.value = true;
  HouseAPI.getHouseContactPage(queryParams)
    .then((data) => {
      tableData.value = data.list || [];
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  queryParams.mobile = "";
  queryParams.ownName = "";
  handleQuery();
}
function handleOpenDialog() {
  formVsisble.value = true;
  currentRow.value = "";
}
async function handleApproval(val, status) {
  const res = await ElMessageBox.confirm(
    `确认${status === BIND_STATUS_ENUM.SUCCESS.value ? "同意" : "拒绝"}吗?`,
    "警告",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  );
  if (!res) return;
  await approval({ id: val.id, status: status });
  handleQuery();
}
async function handleDelete(val) {
  const res = await ElMessageBox.confirm("确认删除吗?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  });

  if (!res) return;
  loading.value = true;
  try {
    // await UserAPI.delete(val.id);
    handleQuery();
    Message.success("删除成功");
  } catch (e) {
    Message.success("删除失败");
  } finally {
    loading.value = false;
  }
}

async function handleChange($event, row) {
  try {
    console.log("row", row);
    const params = {
      status: $event,
      userId: row.id,
    };
    await UserAPI.updateStatus(params);
    handleQuery();
  } catch (e) {
    console.log(e);
  }
}
async function approval(data: Approval) {
  await HouseAPI.updateHouseContactAudit(data);
}
function handleSearch() {
  queryParams.pageNum = 1;
  handleQuery();
}

function changePage(val: any) {
  queryParams.pageNum = val.pageNum;
  queryParams.pageSize = val.pageSize;
  handleQuery();
}
onMounted(async () => {
  handleQuery();
});
</script>
